package org.rsbot.script.methods;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.rsbot.client.Client;
import org.rsbot.script.wrappers.RSComponent;

public class Interfaces extends MethodProvider
{
  private org.rsbot.script.wrappers.RSInterface[] mainCache = new org.rsbot.script.wrappers.RSInterface[0];

  private Map<Integer, org.rsbot.script.wrappers.RSInterface> sparseMap = new HashMap();

  Interfaces(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public synchronized org.rsbot.script.wrappers.RSInterface[] getAll()
  {
    enlargeCache();
    org.rsbot.client.RSInterface[][] arrayOfRSInterface = this.methods.client.getRSInterfaceCache();
    if (arrayOfRSInterface == null)
      return new org.rsbot.script.wrappers.RSInterface[0];
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < arrayOfRSInterface.length; i++) {
      if (arrayOfRSInterface[i] != null) {
        org.rsbot.script.wrappers.RSInterface localRSInterface = get(i);
        if (localRSInterface.isValid()) {
          localArrayList.add(localRSInterface);
        }
      }
    }
    return (org.rsbot.script.wrappers.RSInterface[])localArrayList.toArray(new org.rsbot.script.wrappers.RSInterface[localArrayList.size()]);
  }

  public synchronized org.rsbot.script.wrappers.RSInterface get(int paramInt)
  {
    int i = this.mainCache.length;
    org.rsbot.script.wrappers.RSInterface localRSInterface;
    if (paramInt < i) {
      localRSInterface = this.mainCache[paramInt];
      if (localRSInterface == null) {
        localRSInterface = new org.rsbot.script.wrappers.RSInterface(this.methods, paramInt);
        this.mainCache[paramInt] = localRSInterface;
      }
    } else {
      localRSInterface = (org.rsbot.script.wrappers.RSInterface)this.sparseMap.get(Integer.valueOf(paramInt));
      if (localRSInterface == null) {
        enlargeCache();
        if (paramInt < i) {
          localRSInterface = this.mainCache[paramInt];
          if (localRSInterface == null) {
            localRSInterface = new org.rsbot.script.wrappers.RSInterface(this.methods, paramInt);
            this.mainCache[paramInt] = localRSInterface;
          }
        } else {
          localRSInterface = new org.rsbot.script.wrappers.RSInterface(this.methods, paramInt);
          this.sparseMap.put(Integer.valueOf(paramInt), localRSInterface);
        }
      }
    }
    return localRSInterface;
  }

  public RSComponent getComponent(int paramInt1, int paramInt2)
  {
    return get(paramInt1).getComponent(paramInt2);
  }

  public RSComponent getComponent(int paramInt)
  {
    int i = paramInt >> 16;
    int j = paramInt & 0xFFFF;
    return get(i).getComponent(j);
  }

  public synchronized int getMaxCacheSize()
  {
    enlargeCache();
    return this.mainCache.length;
  }

  public boolean canContinue()
  {
    return getContinueComponent() != null;
  }

  public boolean clickContinue()
  {
    RSComponent localRSComponent = getContinueComponent();
    return (localRSComponent != null) && (localRSComponent.isValid()) && (localRSComponent.doClick(true));
  }

  public RSComponent getContinueComponent()
  {
    if (this.methods.client.getRSInterfaceCache() == null)
      return null;
    org.rsbot.script.wrappers.RSInterface[] arrayOfRSInterface1 = getAll();
    for (org.rsbot.script.wrappers.RSInterface localRSInterface : arrayOfRSInterface1)
      if (localRSInterface.getIndex() != 137) {
        int k = localRSInterface.getChildCount();
        for (int m = 0; m < k; m++) {
          RSComponent localRSComponent = localRSInterface.getComponent(m);
          if ((localRSComponent.containsText("Click here to continue")) && (localRSComponent.isValid()) && (localRSComponent.getAbsoluteX() > 10) && (localRSComponent.getAbsoluteY() > 300))
          {
            return localRSComponent;
          }
        }
      }
    return null;
  }

  public org.rsbot.script.wrappers.RSInterface[] getAllContaining(String paramString)
  {
    LinkedList localLinkedList = new LinkedList();
    for (org.rsbot.script.wrappers.RSInterface localRSInterface : getAll()) {
      if (localRSInterface.getText().toLowerCase().contains(paramString.toLowerCase())) {
        localLinkedList.add(localRSInterface);
      }
    }
    return (org.rsbot.script.wrappers.RSInterface[])localLinkedList.toArray(new org.rsbot.script.wrappers.RSInterface[localLinkedList.size()]);
  }

  public boolean scrollTo(RSComponent paramRSComponent, int paramInt)
  {
    RSComponent localRSComponent = getComponent(paramInt);

    return scrollTo(paramRSComponent, localRSComponent);
  }

  public boolean scrollTo(RSComponent paramRSComponent1, RSComponent paramRSComponent2)
  {
    if ((paramRSComponent1 == null) || (paramRSComponent2 == null) || (!paramRSComponent1.isValid())) {
      return false;
    }
    if (paramRSComponent2.getComponents().length != 6) {
      return true;
    }

    RSComponent localRSComponent1 = paramRSComponent1;
    while ((localRSComponent1.getScrollableContentHeight() == 0) && (localRSComponent1.getParentID() != -1)) {
      localRSComponent1 = getComponent(localRSComponent1.getParentID());
    }

    if (localRSComponent1.getScrollableContentHeight() == 0) {
      return false;
    }

    int i = localRSComponent1.getAbsoluteY();
    int j = localRSComponent1.getRealHeight();

    if ((paramRSComponent1.getAbsoluteY() >= i) && (paramRSComponent1.getAbsoluteY() <= i + j - paramRSComponent1.getRealHeight())) {
      return true;
    }

    RSComponent localRSComponent2 = paramRSComponent2.getComponent(0);
    int k = localRSComponent1.getScrollableContentHeight();

    int m = (int)(localRSComponent2.getRealHeight() / k * (paramRSComponent1.getRelativeY() + random(-j / 2, j / 2 - paramRSComponent1.getRealHeight())));
    if (m < 0)
      m = 0;
    else if (m >= localRSComponent2.getRealHeight()) {
      m = localRSComponent2.getRealHeight() - 1;
    }

    this.methods.mouse.click(localRSComponent2.getAbsoluteX() + random(0, localRSComponent2.getRealWidth()), localRSComponent2.getAbsoluteY() + m, true);

    sleep(random(200, 400));

    while ((paramRSComponent1.getAbsoluteY() < i) || (paramRSComponent1.getAbsoluteY() > i + j - paramRSComponent1.getRealHeight())) {
      int n = paramRSComponent1.getAbsoluteY() < i ? 1 : 0;
      paramRSComponent2.getComponent(n != 0 ? 4 : 5).doAction("");

      sleep(random(100, 200));
    }

    return (paramRSComponent1.getAbsoluteY() >= i) && (paramRSComponent1.getAbsoluteY() <= i + j - paramRSComponent1.getRealHeight());
  }

  private synchronized void enlargeCache()
  {
    org.rsbot.client.RSInterface[][] arrayOfRSInterface = this.methods.client.getRSInterfaceCache();
    if ((arrayOfRSInterface != null) && (this.mainCache.length < arrayOfRSInterface.length)) {
      this.mainCache = ((org.rsbot.script.wrappers.RSInterface[])Arrays.copyOf(this.mainCache, arrayOfRSInterface.length));
      for (int i = this.mainCache.length; i < this.mainCache.length; i++) {
        org.rsbot.script.wrappers.RSInterface localRSInterface = (org.rsbot.script.wrappers.RSInterface)this.sparseMap.get(Integer.valueOf(i));
        if (localRSInterface != null) {
          this.sparseMap.remove(Integer.valueOf(i));
          this.mainCache[i] = localRSInterface;
        }
      }
    }
  }
}